package com.bootdo.common.controller;

import cn.hutool.core.util.IdUtil;
import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileForm;
import com.bootdo.common.service.VideoUploadService;
import com.bootdo.common.utils.Constant;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.VideoTranscodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.validation.Valid;
import java.io.*;
import java.util.Map;

/**
 * @author SiMan
 * @date 2019/11/13 0:02
 */
@Controller
@RequestMapping("/video")
public class VideoTestController {

	@Autowired
	VideoUploadService videoUploadService;

	@Autowired
	BootdoConfig bootdoConfig;

	@GetMapping("")
	public String video() {
		return "videoTest";
	}

	@PostMapping("/isUpload")
	@ResponseBody
	public Map<String, Object> isUpload(@Valid FileForm form) {
		return videoUploadService.findByFileMd5(form.getMd5());
	}


	@GetMapping("/upload")
	public String upload(Map<String, Object> model) {
		return "upload";
	}

	@RequestMapping("/saveUpload")
	@ResponseBody
	public void upload(MultipartHttpServletRequest request) {

		String guid = request.getParameter("guid");
		MultiValueMap<String, MultipartFile> multiFileMap = request.getMultiFileMap();

		MultipartFile multipartFile = multiFileMap.getFirst("file");
		try {
			InputStream in = multipartFile.getInputStream();

			String chunk = request.getParameter("chunk")==null?"0":request.getParameter("chunk");
			String path = bootdoConfig.getUploadPath() + File.separator +
					"video" + File.separator +
					request.getParameter("id") + File.separator + guid;
			File exist = new File(path);
			if (!exist.exists()) {
				exist.mkdirs();
			}
			OutputStream out = new FileOutputStream(path + File.separator + chunk + "_" + request.getParameter("name"));
			byte[] bytes = new byte[1024];
			int len = -1;

			while ((len = in.read(bytes)) != -1) {
				out.write(bytes, 0, len);
			}
			out.close();
			in.close();


		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 文件合并
	 * @param guid
	 * @param realFileName
	 * @param chunks
	 * @param folder
	 */
	@PostMapping("/merge")
	@ResponseBody
	public R merge(String guid, String realFileName, int chunks, String folder ) {
		System.out.println("HE BIN HE BIN HE BIN HE BIN");


		String uuid = IdUtil.simpleUUID();
		String suffix = realFileName.substring(realFileName.lastIndexOf(".") + 1);
		String path = bootdoConfig.getUploadPath()+File.separator+
				"video";
		String path2 = bootdoConfig.getUploadPath()+File.separator+
				"video" + File.separator +
				folder + File.separator +
				guid;
		System.out.println(path);
		try {
			FileOutputStream  fileOutputStream = new FileOutputStream(path+File.separator+uuid + "." + suffix);
			byte[] buf = new byte[1024];
			for(int i=0;i<chunks;i++){
				File tempFile = new File(path2+File.separator+i+"_"+realFileName);
				InputStream inputStream = new FileInputStream(tempFile);
				int len = 0;
				while((len=inputStream.read(buf))!=-1){
					fileOutputStream.write(buf,0,len);
				}
				inputStream.close();
				//删除临时文件
				tempFile.delete();
			}
			fileOutputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return R.ok(uuid);
	}
}

